package co.yixiang.yshop.module.crm.service.crmcloudcalllog;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import co.yixiang.yshop.framework.common.exception.ErrorCode;
import co.yixiang.yshop.framework.redis.util.redis.RedisUtil;
import co.yixiang.yshop.module.crm.cloud.MoorCloudCall;
import co.yixiang.yshop.module.crm.cloud.vo.CallReqVo;
import co.yixiang.yshop.module.crm.enums.CallTypesEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import co.yixiang.yshop.module.crm.controller.admin.crmcloudcalllog.vo.*;
import co.yixiang.yshop.module.crm.dal.dataobject.crmcloudcalllog.CrmCloudcallLogDO;
import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.framework.common.pojo.PageParam;
import co.yixiang.yshop.framework.common.util.object.BeanUtils;

import co.yixiang.yshop.module.crm.dal.mysql.crmcloudcalllog.CrmCloudcallLogMapper;

import static co.yixiang.yshop.framework.common.exception.util.ServiceExceptionUtil.exception;
import static co.yixiang.yshop.module.crm.enums.ErrorCodeConstants.*;

/**
 * 呼叫记录 Service 实现类
 *
 * @author yshop
 */
@Service
@Validated
@Slf4j
public class CrmCloudcallLogServiceImpl implements CrmCloudcallLogService {

    @Resource
    private CrmCloudcallLogMapper cloudcallLogMapper;



    @Override
    public void updateCloudcallLog() {
        String cloudAccountId = RedisUtil.get("cloudAccountId");
        String cloudSecret = RedisUtil.get("cloudSecret");
        if (StrUtil.isEmpty(cloudAccountId) || StrUtil.isEmpty(cloudSecret)) {
            log.error("请先配置云呼叫参数");
            return;
        }
        MoorCloudCall moorCloudCall = new MoorCloudCall(cloudAccountId, cloudSecret);
        //当前时间
        Date date = DateUtil.date();
        Date starDate = DateUtil.offsetHour(date,-1);
        Date endDate = DateUtil.offsetMinute(date,-5);
        JSONObject jsonObject = moorCloudCall.getCCCdr(starDate,endDate);
        List<Dict> list = JSONUtil.toList(JSONUtil.parseArray(jsonObject.get("data")), Dict.class);
        list.forEach(dict -> {
            CrmCloudcallLogDO crmCloudcallLogDO = cloudcallLogMapper.selectOne(new LambdaQueryWrapper<CrmCloudcallLogDO>()
                    .eq(CrmCloudcallLogDO::getActionId,dict.get("ACTION_ID")));
            if(crmCloudcallLogDO != null) {
                crmCloudcallLogDO.setCallNo(dict.get("CALL_NO").toString());
                crmCloudcallLogDO.setStatus(dict.get("STATUS").toString());
                if(dict.get("RECORD_FILE_NAME") != null){
                    crmCloudcallLogDO.setRecordFile(dict.get("FILE_SERVER").toString() + "/" + dict.get("RECORD_FILE_NAME").toString());
                }
                crmCloudcallLogDO.setConnectType(dict.get("CONNECT_TYPE").toString());
                crmCloudcallLogDO.setCallTimeLength(Integer.valueOf(dict.get("CALL_TIME_LENGTH").toString()));
                cloudcallLogMapper.updateById(crmCloudcallLogDO);

                log.info("通话记录更新成功ACTION_ID：{}",dict.get("ACTION_ID"));
            }

        });
       // System.out.println("list:"+list.get(0).get("_id"));
        //cloudcallLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteCloudcallLog(Long id) {
        // 校验存在
        validateCloudcallLogExists(id);
        // 删除
        cloudcallLogMapper.deleteById(id);
    }

    private void validateCloudcallLogExists(Long id) {
        if (cloudcallLogMapper.selectById(id) == null) {
            throw exception(CLOUDCALL_LOG_NOT_EXISTS);
        }
    }

    @Override
    public CrmCloudcallLogDO getCloudcallLog(Long id) {
        return cloudcallLogMapper.selectById(id);
    }

    @Override
    public PageResult<CrmCloudcallLogRespVO> getCloudcallLogPage(CrmCloudcallLogPageReqVO pageReqVO) {
        return cloudcallLogMapper.selectPage(pageReqVO);
    }

}